/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ec.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.ec.entity.ItemSpec;
import com.thinkgem.jeesite.modules.ec.service.ItemSpecService;

/**
 * 规格管理Controller
 * @author zkool
 * @version 2016-03-05
 */
@Controller
@RequestMapping(value = "${adminPath}/ec/itemSpec")
public class ItemSpecController extends BaseController {

	@Autowired
	private ItemSpecService itemSpecService;
	
	@ModelAttribute
	public ItemSpec get(@RequestParam(required=false) String id) {
		ItemSpec entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = itemSpecService.get(id);
		}
		if (entity == null){
			entity = new ItemSpec();
		}
		return entity;
	}
	
	@RequiresPermissions("ec:itemSpec:view")
	@RequestMapping(value = {"list", ""})
	public String list(ItemSpec itemSpec, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ItemSpec> page = itemSpecService.findPage(new Page<ItemSpec>(request, response), itemSpec); 
		model.addAttribute("page", page);
		return "modules/ec/itemSpecList";
	}

	@RequiresPermissions("ec:itemSpec:view")
	@RequestMapping(value = "form")
	public String form(ItemSpec itemSpec, Model model) {
		model.addAttribute("itemSpec", itemSpec);
		return "modules/ec/itemSpecForm";
	}

	@RequiresPermissions("ec:itemSpec:edit")
	@RequestMapping(value = "save")
	public String save(ItemSpec itemSpec, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, itemSpec)){
			return form(itemSpec, model);
		}
		itemSpecService.save(itemSpec);
		addMessage(redirectAttributes, "保存规格管理成功");
		return "redirect:"+Global.getAdminPath()+"/ec/itemSpec/?repage";
	}
	
	@RequiresPermissions("ec:itemSpec:edit")
	@RequestMapping(value = "delete")
	public String delete(ItemSpec itemSpec, RedirectAttributes redirectAttributes) {
		itemSpecService.delete(itemSpec);
		addMessage(redirectAttributes, "删除规格管理成功");
		return "redirect:"+Global.getAdminPath()+"/ec/itemSpec/?repage";
	}

}